from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout
from conan.tools.files import copy, get,chdir
from conan.tools.gnu import Autotools, AutotoolsToolchain
import os
class MxmlRecipe(ConanFile):
    name = "mxml"  # name of the package
    version = "3.3.1"  # version of the package
    license = "Apache-2.0" # license of the package ,lincensa maybe is Apache-2.0, MIT, BSD, etc.
    author = "Michael R Sweet"# author of the package
    url = "https://github.com/michaelrsweet/mxml"# url of the package
    description = ("Mini-XML is a small XML parsing library that you can use to read XML and XML-like data files in your application without requiring large non-standard libraries.")
    topics = ("C", "XML", "parsing", "library")# topics of the package
    settings = "os", "compiler", "build_type", "arch"# settings of the package
    options = {"shared": [True, False], "fPIC": [True, False]}# options of the package
    default_options = {"shared": False, "fPIC": True}# default options of the package
    #exports_sources = "lib_src/*" 


    def source(self):# source of the package
        # Please, be aware that using the head of the branch instead of an immutable tag
        # or commit is a bad practice and not allowed by Conan
        get(self, "https://github.com/michaelrsweet/mxml/archive/refs/tags/v3.3.1.zip",
                  strip_root=True)
        # 如果不加权限,configure()方法有可能无法执行
        os.chmod(os.path.join(self.source_folder, "configure"), 0o755)

        cmake_file_path = os.path.join(self.source_folder, "CMakeLists.txt")
        if os.path.exists(cmake_file_path):
            print("CMakeLists.txt exists: Yes")
        else:
            print("CMakeLists.txt exists: No")

    # def requirements(self):
    #     self.requires("libunwind/1.8.0")
    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC # Windows does not require -fPIC flag

    def layout(self):
        cmake_layout(self)
#        self.folders.source = "lib_src/*"
        
    def generate(self):
        if os.path.exists(os.path.join(self.source_folder, "CMakeLists.txt")):
            tc = CMakeToolchain(self)
        else:
            tc = AutotoolsToolchain(self)
        tc.generate()

    def build(self):
        if os.path.exists(os.path.join(self.source_folder, "CMakeLists.txt")):
            # CMake 构建流程
            print("执行 CMake 构建流程")
            cmake = CMake(self)
            cmake.configure()
            cmake.build()
        else:
            with chdir(self, self.source_folder):
                autotools = Autotools(self)
                autotools.configure(args=["--prefix={}".format(self.package_folder)])
                autotools.make()
    def package(self):
        if os.path.exists(os.path.join(self.source_folder, "CMakeLists.txt")):
            cmake = CMake(self)
            cmake.install()
        else:
            with chdir(self, self.source_folder):
                autotools = Autotools(self)
                autotools.install()
            
    def package_info(self):
        self.cpp_info.libs = ["mxml"]